//
// Created by Liming Shao on 11/21/2017.
//

#include "E243.h"

#include <iostream>
#include <cstring>

int E243::numSum(const char *str) {
    if(str== nullptr)
        return 0;

    int sum = 0, tmp = 0, mark = 1;
    for (int i = 0; i < strlen(str)+1; ++i) {
        if (str[i] <= '9' && str[i] >= '0') {
            tmp = tmp * 10 + (str[i] - '0');
        } else{
            if (tmp != 0) {
                sum += (tmp * mark);
                tmp = 0;
                mark = 1;
            }

            if (str[i] == '-') {
                mark *= -1;
            }else{
                mark = 1;
            }
        }
    }
    return sum;
}

void E243::test() {
    const char *s1 = "A1CD2E33";
    const char *s2 = "A-1B--2C--D6E";
    const char *s3 = "A--300---1+20A10-";
    std::cout << "Num sum of "<< s3 << " = " <<numSum(s1) << std::endl;
}
